package com.youdianstar.app.common.widget.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.RoundedCornersTransformation
import com.youdianstar.app.common.R


/**
 * File: PicListAdapter
 * Author: 82149 Create: 2021/11/11 15:13
 * Changes (from 2021/11/11)
 * --------------------------------------------------
 * description: 图片列表,网点详情用
 * ---------------------------------------------------
 *
 */
class PicListAdapter(val context: Context, val datas: MutableList<String>,val callback : ImageItemListener) :
    RecyclerView.Adapter<PicListAdapter.ImageHolder>(){

    interface ImageItemListener{
        fun onItemClick(position :Int)
    }

    class ImageHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val ivIcon by lazy {
            itemView.findViewById<ImageView>(R.id.item_image)
        }
    }

    /**刷新数据*/
    fun refreshData(list: List<String>) {
        datas.clear()
        datas.addAll(list)
        this.notifyDataSetChanged()
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_item_image_banner, parent, false)
        return ImageHolder(view)
    }

    override fun onBindViewHolder(holder: ImageHolder, position: Int) {
        holder.ivIcon.load(datas[position]){
            transformations(
                RoundedCornersTransformation(topLeft = 5f, topRight = 5f,bottomLeft = 5f, bottomRight = 5f)
            )
            error(R.drawable.ic_img_picker_default)
            placeholder(R.drawable.ic_img_picker_default)
        }
        holder.ivIcon.setOnClickListener {
            callback.onItemClick(position)
        }
    }

    override fun getItemCount(): Int {
        return datas.size
    }
}